﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class DevMoveNote : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(DevMoveNote));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public string sFileName = GlobleClass.UpFileType.DevMovefile.ToString(); //"DevMovefile";

        #region 载入信息

        public void loadInfo()
        {
            BuyDevice buy = null;
            string sModelName = "";
            DataSet ds = null;
            try
            {
                buy = new BuyDevice();
                DeviceInfo devinfo = new DeviceInfo();
                //inniDdlData(ddlPayWay,buy.getEnumDdl(2));
                if (m_Id == 0)
                {
                    if (!checkModelRightById(104, ref sModelName, (clsBaseLib)buy))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(105, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(106, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = devinfo.getDevMoveInfoById(m_Id);//buy.getContractInfoById(m_Id);
                    //hdselect.Value = buy.getContractSelectIds(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        txtMoveCode.Text = Convert.ToString(dt.Rows[0]["DiaoboID"]);
                        txtBfDeptId.Text = Convert.ToString(dt.Rows[0]["BeforDeptId"]);
                        txtBfDeptName.Text = Convert.ToString(dt.Rows[0]["DeptName"]);
                        txtAfDeptId.Text = Convert.ToString(dt.Rows[0]["AfterDeptId"]);
                        txtAfDeptName.Text = Convert.ToString(dt.Rows[0]["AfDeptName"]);
                        txtMoveDate.Text = Convert.ToDateTime(dt.Rows[0]["dProcessTime"]).ToString("yyyy-MM-dd");
                        txtContent.Text = Convert.ToString(dt.Rows[0]["DiaoboReason"]);
                        //txtStartDate.Text = Convert.ToDateTime(dt.Rows[0]["startdate"]).ToString("yyyy-MM-dd");
                        //txtEndDate.Text = Convert.ToDateTime(dt.Rows[0]["enddate"]).ToString("yyyy-MM-dd");
                        TxtMoveState.Text = Convert.ToString(dt.Rows[0]["DiaoboStatus"]);
                        //txtUserDept.Text = Convert.ToString(dt.Rows[0]["userdeptname"]);
                        //txtUserName.Text = Convert.ToString(dt.Rows[0]["username"]);
                        //ddlPayWay.SelectedValue = Convert.ToString(dt.Rows[0]["payway"]);
                        //txtPayMoney.Text = Convert.ToString(dt.Rows[0]["paymoney"]);
                        //ddlIsJF.SelectedValue = Convert.ToString(dt.Rows[0]["isjf"]);
                        //tdupload.InnerHtml = Convert.ToString(dt.Rows[0]["contractfile"]);
                    }
                    DataTable dtDev = new DataTable();
                    dtDev = ds.Tables[1];
                    int iRowCount = dtDev.Rows.Count;
                    StringBuilder sbu = new StringBuilder();
                    for (int i = 0; i < iRowCount; i++)
                    {
                        sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removedevice(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" onclick=""getdevicetype(this)"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
                        <td width=""150"">
                            <input style=""width: 90px"" type=""text"" value=""{4}"" /></td>
                    </tr>", "",dtDev.Rows[i]["DeviceCode"], dtDev.Rows[i]["DeviceName"], dtDev.Rows[i]["BuyPrice"], dt.Rows[i]["DeptName"]));
                    }
                    ltrDevice.Text = Convert.ToString(sbu);
                }
            }
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 获得设备

        [Ajax.AjaxMethod]
        public DataTable getSelectDeviceInfo(string sIds)
        {
            DeviceInfo devinfo = null;
            DataTable dt = null;
            try
            {
                devinfo = new DeviceInfo();
                dt = devinfo.getResultDeviceMoviInfo(sIds);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                if (devinfo != null)
                {
                    devinfo.Dispose();
                }
            }
            return dt;
        }

        #endregion

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        //public bool saveData(int iContractId,string sCode,string sCheckDate, string sContractName, int iPayWay, string sPayMoney, string sUserDept, string sUName
        //    ,int iIsJF, int iCompanyId, string sCompanyUser, string sStartDate, string sEndDate, string sContent, string sMemo, string sFiles, string sSelectIds, string[] sDevices)
        public bool saveData(int imid, string smcode, string smdate, int imbfid, int imafid, string smreason, string smstate, string sFiles, string[] sDevices)
        {
            DeviceInfo devinfo = null;
            bool bResult = false;
            smcode = smcode.Replace("'", "''");
            smdate = smdate.Replace("'", "''");
            smreason = smreason.Replace("'", "''");
            smstate = smstate.Replace("'", "''");
            sFiles = sFiles.Replace("'", "''");
            try
            {
                devinfo = new DeviceInfo();

                for (int i = 0; i < sDevices.Length; i++)
                {
                    sDevices[i] = sDevices[i].Replace("'", "''");
                }
                bResult = devinfo.setMoveInfo(imid, smcode, smdate, imbfid, imafid, smreason, smstate, sFiles, sDevices);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (devinfo != null)
                {
                    devinfo.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}